package com.zhaolang.qogir.ui;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.umeng.message.PushAgent;
import com.zhaolang.qogir.R;
import com.zhaolang.qogir.application.ActivityManager;
import com.zhaolang.qogir.application.QogirApplication;
import com.zhaolang.qogir.domain.BaseCallNode;
import com.zhaolang.qogir.ui.ActivityHelper.StopStateable;
import com.zhaolang.qogir.util.FlippingLoadingDialog;
import com.zhaolang.qogir.util.UmengUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import butterknife.ButterKnife;

public class BaseActivity extends Activity {

    private final static String TAG = BaseActivity.class.getName();
    private final static boolean DEBUG = true;

    private ActivityHelper mHelpActivity;
    private QogirApplication mApplication;
    private ActivityManager activityManager = null;

    private BaseCallbackManager mCallbackManager;
    protected FlippingLoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* 此方法与统计分析sdk中统计日活的方法无关，需要调用此方法 */
        PushAgent.getInstance(QogirApplication.getContext()).onAppStart();
        mHelpActivity = new ActivityHelper(this);
        mCallbackManager = new BaseCallbackManager();
        mApplication = (QogirApplication) getApplicationContext();
        //将当前Activity压入栈
        activityManager = ActivityManager.getActivityManagerIntance(this);
        activityManager.pushTask(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.test_primary);//通知栏所需颜色
        }
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    public <T extends View> T findViewById_(int id) {
        return (T) findViewById(id);
    }

    public void addCallback(BaseCallNode node) {
        mCallbackManager.addCallbace(node);
    }

    /**
     * 显示等待对话框
     *
     * @param task 你的异步任务，如果用户点击Back键，可以取消正在执行的异步任务.没有可以为null
     */
    protected void showWaitingDialog(final AsyncTask<?, ?, ?> task) {
        mHelpActivity.showWaitingDialog(task);
    }

    /**
     * 显示等待对话框
     *
     * @param contextState 你的状态模式，如果用户点击Back键，并且当前正在执行的State实现了{@link StopStateable},将会调用
     *                     {@link StopStateable#stop()}
     */
    protected void showWaitingDialog(final ContextState contextState) {
        mHelpActivity.showWaitingDialog(contextState);
    }

    /**
     * 设置等待的文本
     *
     * @param text
     */
    protected void setWaitingText(CharSequence text) {
        mHelpActivity.setWaitingText(text);
    }

    /**
     * 关闭等待对话框
     */
    protected void dimessWaitingDialog() {
        mHelpActivity.dimessWaitingDialog();
    }

    protected void i(String msg) {
        if (DEBUG)
            Log.i(TAG, msg);
    }

    protected void w(String msg) {
        Log.w(TAG, msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityManager.removeTask(this);
        mCallbackManager.clearCallback();
    }

    @Override
    protected void onResume() {
        super.onResume();
        UmengUtils.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        UmengUtils.onPause(this);
    }

    public FlippingLoadingDialog getLoadingDialog(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new FlippingLoadingDialog(this);
            mLoadingDialog.setText(msg);
        }
        return mLoadingDialog;
    }

    /* hide soft input UI, children class can implement this interface */
    public void removeSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView()
                    .getWindowToken(), 0);
        }
    }
}
